package com.example.mydy;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.mydy.model.Announcement;
import com.example.mydy.model.RewardTask;
import com.example.mydy.model.User;
import com.example.mydy.model.UserRanking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ActivityManager {
    private static final String TAG = "ActivityManager";
    private static ActivityManager instance;
    private final Context context;
    private Map<String, Object> activityTestData;

    private ActivityManager(Context context) {
        this.context = context.getApplicationContext();
        this.activityTestData = new HashMap<>();
    }

    public static synchronized ActivityManager getInstance(Context context) {
        if (instance == null) {
            instance = new ActivityManager(context);
        }
        return instance;
    }

    public void generateTestData() {
        Random random = new Random();
        
        // 设置测试数据
        activityTestData.put("today_income", 25.5 + random.nextDouble() * 10);
        activityTestData.put("completed_tasks", 3 + random.nextInt(5));
        activityTestData.put("downline_income", 18.75 + random.nextDouble() * 5);
        activityTestData.put("downline_count", 5 + random.nextInt(10));
        
        Log.d(TAG, "生成测试数据: " + activityTestData);
    }

    public User getCurrentUser() {
        // 从SharedPreferences获取当前用户信息
        SharedPreferences prefs = context.getSharedPreferences("MyAppPrefs", Context.MODE_PRIVATE);
        String username = prefs.getString("username", "");
        String nickname = prefs.getString("userNickname", "");
        int userId = prefs.getInt("userId", 0);
        
        User user = new User();
        user.setId(userId);
        user.setUsername(username);
        user.setNickname(nickname);
        
        return user;
    }

    public double getTodayIncome(String username) {
        // 返回测试数据或从服务器获取
        return activityTestData.containsKey("today_income") ? 
               (double) activityTestData.get("today_income") : 25.5;
    }

    public int getCompletedTaskCount(String username) {
        // 返回测试数据或从服务器获取
        return activityTestData.containsKey("completed_tasks") ? 
               (int) activityTestData.get("completed_tasks") : 3;
    }

    public double getTodayDownlineIncome(String username) {
        // 返回测试数据或从服务器获取
        return activityTestData.containsKey("downline_income") ? 
               (double) activityTestData.get("downline_income") : 18.75;
    }

    public List<Announcement> getAnnouncements(int limit) {
        // 创建示例公告列表
        List<Announcement> announcements = new ArrayList<>();
        for (int i = 1; i <= limit; i++) {
            Announcement announcement = new Announcement();
            announcement.setId(i);
            announcement.setTitle("系统公告 " + i);
            announcement.setContent("这是一个系统公告示例内容，包含重要信息...");
            announcement.setCreateTime("2023-05-" + (10 + i));
            announcements.add(announcement);
        }
        return announcements;
    }

    public List<RewardTask> getHotRewards(int limit) {
        // 创建示例热门悬赏任务
        List<RewardTask> rewards = new ArrayList<>();
        for (int i = 1; i <= limit; i++) {
            RewardTask reward = new RewardTask();
            reward.setId(i);
            reward.setTitle("热门悬赏任务 " + i);
            reward.setDescription("完成此任务可获得奖励");
            reward.setReward(10.0 * i);
            reward.setRemainingOrders(5 - (i % 3));
            reward.setEarnedPeople(10 + i);
            rewards.add(reward);
        }
        return rewards;
    }
    
    public List<UserRanking> getTopUsers(int limit) {
        // 创建示例用户排行榜
        List<UserRanking> rankings = new ArrayList<>();
        for (int i = 1; i <= limit; i++) {
            UserRanking ranking = new UserRanking();
            ranking.setUserId(i);
            ranking.setUsername("用户" + i);
            ranking.setNickname("达人" + i);
            ranking.setEarnings(1000.0 - i * 50);
            ranking.setTasksCompleted(50 - i * 2);
            ranking.setRanking(i);
            rankings.add(ranking);
        }
        return rankings;
    }
} 